﻿// See https://aka.ms/new-console-template for more information
//Console.WriteLine("Hello, World!");

using CSharp10;
using CSharp10.GenericClass;
using CSharp10.NetFundamentals;
using CSharp10.XMLDocumentation;
using Microsoft.Data.SqlClient;
using System.Text.Json;
using System.Text.Json.Nodes;

//GenericTypes.Show();
//StringAndTextHanding.Show();

/*string[] players = { "Tom", "Jay", "Mary" };

var query1 = from name1 in players
             from name2 in players
             select name1 + " vs " + name2;
foreach (var layer in query1)
{
    Console.WriteLine(layer);
}

Console.WriteLine("************************************");

var query2 = from name1 in players
             from name2 in players
             where name1.CompareTo(name2) <0
             orderby name1,name2
             select name1 + " vs "+ name2;
foreach (var layer in query2)
{
    Console.WriteLine(layer);
}
Console.WriteLine(string.Join(',',query2));

var avg = new int[] { 3, 4 }.Average();
int[] nums = { 1, 3, 4, 5, 7 };
double avgs = nums.Average(n=>(double)n);
Console.WriteLine(avgs);
Console.WriteLine();
int[] numbers = { 2, 3, 4 };
int sum = numbers.Aggregate(0,(total,n)=>total+n*n);
Console.WriteLine(sum);

int[] nums = { 2, 3, 4 };
bool hasABigNumber = nums.Any(n => n > 10);
Console.WriteLine(hasABigNumber);
bool hasBigNumber = nums.Where(n => n > 10).Any();
Console.WriteLine(hasBigNumber);

int[][] numbers =
{
    new int[] { 1, 2 ,3},
    new int[]{5,6,7},
    null
};
IEnumerable<int> flat = numbers.SelectMany(n => n??Enumerable.Empty<int>());
Console.WriteLine(string.Join(',',flat));

foreach(int i in Enumerable.Range(5,3))
    Console.Write(i+" ");
Console.WriteLine();
foreach (bool x in Enumerable.Repeat(false, 3))
    Console.Write(x + " ");*/

//XMLDocTags.Show2();

/*byte[] data = File.ReadAllBytes("people.json");
Utf8JsonReader reader = new Utf8JsonReader(data);
while (reader.Read())
{
    switch (reader.TokenType)
    {
        case JsonTokenType.StartObject:
            Console.WriteLine($"Start of object");
            break;
        case JsonTokenType.EndObject:
            Console.WriteLine($"End of object");
            break;
        case JsonTokenType.StartArray:
            Console.WriteLine();
            Console.WriteLine($"Start of array");
            break;
        case JsonTokenType.EndArray:
            Console.WriteLine("End of array");
            break;
        case JsonTokenType.PropertyName:
            Console.WriteLine($"Property:{reader.GetString()}");
            break;
        case JsonTokenType.String:
            Console.WriteLine($" Value:{reader.GetString()}");
            break;
        case JsonTokenType.Number:
            Console.WriteLine($" Value:{reader.GetInt32()}");
            break;
        default:
            Console.WriteLine($"No Support for {reader.TokenType}");
            break;
    }
*/
// JsonWriter

/*    var options = new JsonWriterOptions { Indented = true };
    using(var stream = File.Create("MyFile.json"))
    using (var writer = new Utf8JsonWriter(stream, options))
    {
        writer.WriteStartObject();
        // 一次性写入 属性 和对应的值
        writer.WriteString("FirstName", "Dylan");
        writer.WriteString("LastName", "Lockwood");
        // 分别写属性 和对应的值
        writer.WritePropertyName("Age");
        writer.WriteNumberValue(23);
        writer.WriteCommentValue("This is a non-standard comment");
        writer.WriteEndObject();
    }*/


/*using JsonDocument document = JsonDocument.Parse("123");
JsonElement root = document.RootElement;
Console.WriteLine(root.ValueKind);
int num = document.RootElement.GetInt32();
Console.WriteLine(num);

// 读取数组
using JsonDocument document = JsonDocument.Parse("[1,2,3,4,5,6,7]");
int length = document.RootElement.GetArrayLength();
int val = document.RootElement[3].GetInt32();
Console.WriteLine(length);
Console.WriteLine(val);


// 读取对象
using JsonDocument document = JsonDocument.Parse(@"{""Age"":32}");
JsonElement root = document.RootElement;
int age = root.GetProperty("Age").GetInt32();
Console.WriteLine(age);
JsonProperty ageProp = root.EnumerateObject().First();
string name = ageProp.Name;
JsonElement value  = ageProp.Value;
Console.WriteLine(value.ValueKind);
Console.WriteLine(value.GetInt32());
Console.WriteLine(name);
*/


/*using var stream = File.OpenRead("Persons.json");
using JsonDocument document = JsonDocument.Parse(stream);

var query = from person in document.RootElement.EnumerateArray()
            select new
            {
                FirstName = person.GetProperty("FirstName").GetString(),
                Age = person.GetProperty("Age").GetInt32(),
                Friends = from friend in person.GetProperty("Friends").EnumerateArray()
                          select friend.GetString()
            };

foreach (var item in query)
{
    Console.WriteLine($"FirstName:{item.FirstName}");
    Console.WriteLine($"Age:{item.Age}");
    Console.WriteLine("Friends:");
    foreach (var item2 in item.Friends)
    {
        Console.Write($"  {item2}");

    }
    Console.WriteLine();
}
*/


/*using var json = File.OpenRead("Persons.json");
using JsonDocument document = JsonDocument.Parse(json);
var options = new JsonWriterOptions { Indented = true };
using(var outputStream = File.Create("NewFile.json"))
using (var writer = new Utf8JsonWriter(outputStream, options))
{
    writer.WriteStartArray();
    foreach(var person in document.RootElement.EnumerateArray())
    {
        int friendCount = person.GetProperty("Friends").GetArrayLength();
        if(friendCount >=2 )
        {
            person.WriteTo(writer);
        }
    }
    writer.WriteEndArray();
}

*/

/*var node = JsonNode.Parse("123");
int num = node.AsValue().GetValue<int>();
Console.WriteLine(num);

int number = node.GetValue<int>();
Console.WriteLine(number);

var n = (int)node;
Console.WriteLine(n);*/

/*var node = JsonNode.Parse("[1,2,3,4,5]");
//Console.WriteLine(node.AsArray().Count);
//foreach(var child in node.AsArray())
//    Console.WriteLine(child);
Console.WriteLine((int)node[2]);*/


/*var node = JsonNode.Parse(@"{""Name"":""Alice"",""Age"":32}");
//string name = (string)node["Name"];
//int age = (int)node["Age"];
//Console.WriteLine(name);
//Console.WriteLine(age);

foreach(var k in node.AsObject())
{
    string propertyName = k.Key;
    JsonNode value= k.Value;
}
*/
/*using var json = File.OpenRead("Persons.json");
var node = JsonNode.Parse(json);
string li = (string)node[1]["Friends"][2];
Console.WriteLine(li);
*/

// 读取
/*var node = JsonNode.Parse(File.ReadAllText("Persons.json"));
var query = from person in node.AsArray()
            select new {
                FirstName = (string)person["FirstName"],
                Age = (int)person["Age"],
                Friends = from friend in person["Friends"].AsArray()
                          select (string)friend
            };

foreach (var item in query)
{
    Console.WriteLine($"FirstName:{item.FirstName}");
    Console.WriteLine($"Age:{item.Age}");
    Console.WriteLine("Friends:");
    foreach (var item2 in item.Friends)
    {
        Console.Write($"  {item2}");

    }
    Console.WriteLine();
}*/

// 更新
/*//var node = JsonNode.Parse("{\"Color\":\"Red\"}");
//node["Color"] = "white";
//node["Valid"] = true;
//Console.WriteLine(node.ToJsonString());
//node.AsObject().Remove("Valid");
//Console.WriteLine(node.ToJsonString());

//var node = JsonNode.Parse("[1,2,3,4]");
//node[0] = 10;
//node.AsArray().RemoveAt(1);
//node.AsArray().Add(12);
//Console.WriteLine(node.ToJsonString());
//foreach (var item in node.AsArray()) Console.WriteLine(item);

var node = new JsonArray()
{
    new JsonObject
    {
        ["Name"] = "Tracy",
        ["Age"] = 30,
        ["Friends"] = new JsonArray("Lisa","Joe")
    },
    new JsonObject
    {
        ["Name"] = "Jordyn",
        ["Age"] = 25,
        ["Friends"] = new JsonArray("Tracy","Li")
    }
};

Console.WriteLine(node.ToJsonString());*/

//Console.WriteLine("你好 王梦阔");

//Console.ReadLine();


//await DiagnosticsTest.Show();

//Thread worker = new Thread(()=>Console.ReadLine());
//if(args.Length>0) worker.IsBackground = true;
//worker.Start();

//await ConcurrencyAndMultiThresd.Show();

//var instance = new ConcurrencyAndMultiThresd();
//await instance.Show5();

//var instance = new SystemIOClasses();
//instance.Show();

AssemblyTest.Show();

//static SqlConnection GetConnection() => new SqlConnection();
//GetConnection();



